//
//  SearchViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/17.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "SearchViewController.h"
#import "UIView+Extension.h"
@interface SearchViewController ()<UITextFieldDelegate>

@property (nonatomic ,strong) SBView  * searchBar;
@property (nonatomic ,strong) UIView  * scView;
@property (nonatomic ,strong) UIView  * btnView;
@property (nonatomic ,strong) NSArray * rankArray;
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.rankArray=[NSArray arrayWithObjects:@"男巫是打了大飒飒啦",@"屌丝",@"高富帅",@"龙凤呈祥龙凤呈祥",@"宇宙无敌超级大屌丝",@"校花的贴身校园高寿",@"中观人们共和国安徽省琥珀分理处",@"悠悠的撒的奥多", nil];
    [self createdUI];
    [self creatSearchView];
    
    // Do any additional setup after loading the view.
}

- (void)createdUI
{
    //156  28
    self.searchBar= [[SBView alloc] initWithFrame:CGRectMake(0, 5, MainW-120, 28)];
    self.searchBar.textfied.delegate=self;
    self.navigationItem.titleView = self.searchBar;
    UIBarButtonItem*item=[[UIBarButtonItem alloc]initWithTitle:@"搜索" style:UIBarButtonItemStyleDone target:self action:@selector(searchBtnClick)];
    [item setTitleTextAttributes:@{NSFontAttributeName:FontAndStyle14,NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem=item;

    
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField endEditing:YES];
    return YES;
}
//搜索
-(void)searchBtnClick{
    [self.searchBar endEditing:YES];
}
-(void)creatSearchView{
    self.scView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH-64)];
    self.scView.backgroundColor=Gray_Color;
    [self.view addSubview:self.scView];
    self.btnView=[[UIView alloc]initWithFrame:CGRectMake(0, 40, MainW, MainH-64-40)];
    self.btnView.backgroundColor=[UIColor clearColor];
    [self.scView addSubview:self.btnView];
    MYLabel *label=[[MYLabel alloc]initWithFrame:CGRectMake(15, 15, MainW-30, 15)];
    label.text=@"最近搜索";
    label.font=[UIFont systemFontOfSize:15];
    label.textColor=TextColor3;
    [self.scView addSubview:label];
    UIImageView*iconDele=[[UIImageView alloc]initWithFrame:CGRectMake(MainW-20, label.bsg_y-1, 10, 12)];
    iconDele.image=[UIImage imageNamed:@"icon_delete"];
    [self.scView addSubview:iconDele];
    UIButton *deletebtn=[[UIButton alloc]initWithFrame:CGRectMake(MainW-40,0 , 40, 40)];
    [deletebtn addTarget:self action:@selector(deleteBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scView addSubview:deletebtn];
    int count = 0;
    float btnWidth = 0;
    float viewHeight = 0;
    for (int i = 0; i < self.rankArray.count; i++) {
        
        NSString *btnName = self.rankArray[i];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundColor:[UIColor whiteColor]];
        [btn setTitleColor:TextColor3 forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        btn.titleLabel.font = FontAndStyle12;
        [btn setTitle:btnName forState:UIControlStateNormal];
        //btn.titleLabel.lineBreakMode =  NSLineBreakByWordWrapping;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.layer.cornerRadius = 5;
        btn.layer.masksToBounds = YES;
        
        //NSDictionary *dict = [NSDictionary dictionaryWithObject:FONT(12) forKey:NSFontAttributeName];
        CGSize btnSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font }];
        
        btn.width = btnSize.width + 15;
        btn.height = btnSize.height + 12;
        
        if (i==0)
        {
            btn.x = 25;
            btnWidth += CGRectGetMaxX(btn.frame);
        }
        else{
            btnWidth += CGRectGetMaxX(btn.frame)+20;
            if (btnWidth > MainW) {
                count++;
                btn.x = 25;
                btnWidth = CGRectGetMaxX(btn.frame);
            }
            else{
                
                btn.x += btnWidth - btn.bsg_width;
            }
        }
        btn.y += count * (btn.bsg_height+10)+10;
        
        viewHeight = CGRectGetMaxY(btn.frame)+10;
        
        [self.btnView addSubview:btn];
        
        btn.tag = 100+i;
        
        
        //        if ([btnName isEqualToString:self.selectStr])
        //        {
        //            self.selectBtn = btn;
        //            self.selectBtn.selected = YES;
        //            self.selectBtn.backgroundColor = [UIColor greenColor];
        //        }
        
    }

}
-(void)deleteBtnClick{
    LXAlertView *alertView=[[LXAlertView alloc]initWithTitle:@"" message:@"确定删除历史搜索记录?" cancelBtnTitle:@"取消" otherBtnTitle:@"确定" clickIndexBlock:^(NSInteger clickIndex) {
        
    }];
    [alertView showLXAlertView];
}
-(void)btnClick:(UIButton *)sender{
    self.searchBar.textfied.text=self.rankArray[sender.tag-100];
    LXAlertView *alertView=[[LXAlertView alloc]initWithTitle:@"" message:@"想搜什么啊  宇宙无敌超级大屌丝" cancelBtnTitle:@"你才是屌丝" otherBtnTitle:@"我就是屌丝" clickIndexBlock:^(NSInteger clickIndex) {
        
    }];
    [alertView showLXAlertView];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.searchBar endEditing:YES];
}
@end
